﻿
using System;
using System.Drawing;
using System.Windows.Forms;
using n_LocatePanel;
using n_MainSystemData;
using System.Management;
using n_SysID;
using n_DR;

namespace n_PassForm
{
/// <summary>
/// Description of LocateForm.
/// </summary>
public partial class PassForm : Form
{
	//已使用的天数
	public double UsedTime;
	
	//剩余的天数
	public double LastTime;
	
	//距离最后一次使用功能的时间
	public double FromLastTime;
	
	string PackID;
	
	string SYS_ID;
	
	int ErrorTime;
	
	public PassForm()
	{
		//
		// The InitializeComponent() call is required for Windows Forms designer support.
		//
		InitializeComponent();
		
		ErrorTime = 0;
		
		DR.Init();
		PackID = UserDR.GetNewID();
		textBoxID.Text = "A10-" + PackID;
		
		try {
			SysID.Init();
			SYS_ID = SysID.GetInfo();
			if( SYS_ID != UserData.CPU_ID ) {
				//UserData.StartDate = DateTime.Now;
				//UserData.CPU_ID = SYS_ID;
				//UserData.TotalTime = 0;
				//UserData.isChanged = true;
				//MessageBox.Show( "系统检测到您的电脑为第一次安装linkboy软件, 将会给您提供7天的临时内测用户特权, 7天内您可以使用软件的全部内测功能." );
			}
		}
		catch {
			MessageBox.Show( "Ident读取失败" );
			//...
		}
		RefreshTime();
	}
	
	//判断当前是否为有效时间
	public bool isOk()
	{
		if( UserData.CPU_ID == SYS_ID && LastTime > 0 && FromLastTime > 0 ) {
			return true;
		}
		return false;
	}
	
	//更新最后使用时间
	public void RefreshLastTime()
	{
		UserData.LastDate = DateTime.Now;
		UserData.isChanged = true;
	}
	
	//运行
	public void Run()
	{
		MessageBox.Show( "此功能目前处于内测状态, 您不是linkboy内测用户, 暂时无法使用此功能" );
		
		
		//判断是否经过修改时间
		if( FromLastTime < 0 ) {
			labelMes.Text = "您的计算机可能修改过时间, 导致激活码失效, 如需启用请将电脑时间调整到此时间以后:" + UserData.LastDate;
		}
		//判断授权是否过期
		if( LastTime <= 0 ) {
			labelMes.Text = "您的激活码已过有效期, 如需使用请重新激活.";
		}
		this.Visible = true;
	}
	
	void RefreshTime()
	{
		TimeSpan span = DateTime.Now - UserData.StartDate;
		UsedTime = span.TotalDays;
		LastTime = UserData.TotalTime - UsedTime;
		
		TimeSpan span1 = DateTime.Now - UserData.LastDate;
		FromLastTime = span1.TotalDays;
	}
	
	void LocateFormFormClosing(object sender, FormClosingEventArgs e)
	{
		this.Visible = false;
		e.Cancel = true;
	}
	
	void Button1Click(object sender, EventArgs e)
	{
		if( ErrorTime >= 3 ) {
			MessageBox.Show( "验证出错次数过多, 系统已锁定" );
			return;
		}
		
		try {
			
			int m = 0;
			if( UserDR.DecodeOK( textBoxYanZheng.Text, PackID, ref m ) ) {
				UserData.TotalTime = m;
				
				//用户注册成功后执行这个
				UserData.StartDate = DateTime.Now;
				UserData.CPU_ID = SYS_ID;
				UserData.isChanged = true;
				RefreshTime();
				
				MessageBox.Show( "验证通过, 您已拥有内测用户特权, 现在可以使用软件中的所有内测体验功能, 有效期为" + UserData.TotalTime + "天" );
				
				this.Visible = false;
			}
			else {
				MessageBox.Show( "验证失败" );
				ErrorTime++;
			}
		}
		catch {
			MessageBox.Show( "请输入数字" );
			return;
		}
	}
}
}



